package  com.example.demo1.activity
import ArticleViewModel
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.demo1.R
import com.example.demo1.adapter.ArticleListAdapter
import com.example.demo1.adapter.OptionAdapter
import com.example.demo1.databinding.ActivityArtitleBinding
import com.google.android.material.tabs.TabLayout
import kotlinx.coroutines.launch

var selectNum:MutableLiveData<Int> =MutableLiveData<Int>(650)
var typeNum=MutableLiveData<Int>(1)
/**
 * 高阶英语文库界面activity
 * **/
class ArtitleActivity : AppCompatActivity() {
    private val viewModel: ArticleViewModel by viewModels()
    private lateinit var binding: ActivityArtitleBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityArtitleBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.tabLayout.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                selectNum.value= tab!!.position
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
        viewModel.fetchData()
        lifecycleScope.launch {
            viewModel.typeListStateFlow.collect { typeList ->
                if (typeList.isNotEmpty()) {
                    val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
                    recyclerView.layoutManager = LinearLayoutManager(this@ArtitleActivity)
                    recyclerView.adapter = OptionAdapter(typeList)
                }
            }
        }


        lifecycleScope.launch {
            viewModel.selectListStateFlow.collect { selectList ->
                if (selectList.isNotEmpty()) {
                    binding.tabLayout.setTitle(selectList)
                    binding.tabLayout.getTabAt(129)?.select()
                    binding.tabLayout.post { binding.tabLayout.scrollTo(binding.tabLayout.getTabAt(129)?.view?.left ?: 0, 0) }
                }
            }
        }


        lifecycleScope.launch {
            viewModel.articleListStateFlow.collect { articleList ->
                if (articleList!= null) {
                    binding.artitleRecycler.visibility= View.VISIBLE
                    binding.alarmTextView.visibility= View.INVISIBLE
                    Log.d("ArticleViewModel", "onResponse: $articleList")
                    binding.artitleRecycler.layoutManager = LinearLayoutManager(this@ArtitleActivity)
                    binding.artitleRecycler.adapter = ArticleListAdapter(this@ArtitleActivity,articleList)
                }
                else{
                    binding.artitleRecycler.visibility= View.INVISIBLE
                    binding.alarmTextView.visibility= View.VISIBLE

                }
            }
        }
        selectNum.observe(this
        ) {
            viewModel.refreshArtitleList(  (binding.tabLayout.selectedTabPosition+1)*5!!,typeNum.value!!)
//            Log.d("ArticleViewModel", "onResponse: $it"+typeNum.value!!+ selectNum.value!!)
        }
        typeNum.observe(this
        ) {
//            Log.d("ArticleViewModel", "onResponse: $it")
            viewModel.refreshArtitleList((binding.tabLayout.selectedTabPosition+1)*5!!,typeNum.value!!)
        }
        binding.swiperefresh.setOnRefreshListener {
            binding.swiperefresh.setRefreshing(false)
        }
    }
}